package com.youge.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @author: hcf
 * @qq: 46914685
 * @email: 46914685@qq.com
 * @date: 2020-10-11 20:28
 */

/**
 * 死信测试
 *消费者拒接消费消息,basicNack/basicReject,并且不把消息重新放入原目标队列,requeue=false
 */
@Component
public class OrderListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(1000);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //1.接收转换消息
            System.out.println(new String(message.getBody()));
            //2.处理业务逻辑
            System.out.println("处理业务逻辑....");
            System.out.println("根据订单id查询其状态...");
            System.out.println("判断状态是否为支付成功");
            System.out.println("取消订单,回滚库存...");

            //3.手动签收
            /**
             * basicAck(long deliveryTag, boolean multiple)
             * multiple 是否处理多条
             */
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
//            e.printStackTrace();
//            4.拒绝签收
            /**
             * basicNack(long deliveryTag, boolean multiple, boolean requeue)
             * requeue:重回队列.如果设置为true,则消息重新回到queue,broker会重新发送
             *         该消息给消费端
             */
            System.out.println("出现错误,拒绝签收....");
            channel.basicNack(deliveryTag, true, true);
        }

    }
}
